package com.ruoyi.merchant.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.merchant.domain.Orders;
import com.ruoyi.merchant.domain.ServiceDetails;
import com.ruoyi.merchant.domain.TotalAmount;
import com.ruoyi.merchant.service.OrdersService;
import com.ruoyi.merchant.service.ServiceDetailsService;
import com.ruoyi.merchant.service.TotalAmountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @name: verificationRecordController
 * @author: zhangzhimin
 * @date: 2025-05-19 14:36
 **/
@RestController
@RequestMapping("/verificationRecord")
public class verificationRecordController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private ServiceDetailsService serviceDetailsService;
    @Autowired
    private TotalAmountService totalAmountService;

    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/list/{id}")
    public List<Map<String, Object>> list(@PathVariable("id") Long id) {
        return ordersService.getVerificationRecordByMid(id);
    }

    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/list/{id}/{username}")
    public List<Map<String, Object>> list(@PathVariable("id") Long id, @PathVariable("username") String username) {
        return ordersService.getVerificationRecordByusername(username, id);
    }

    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/writeOff/{id}/{code}")
    public int writeOff(@PathVariable("id") Long id, @PathVariable("code") String code) {
        Orders orders = ordersService.selectOrderverificationRecordByCode(id, code);
        if (orders.getOrderStatus() != 0) {
            return orders.getOrderStatus();
        }
        orders.setOrderStatus(1);
        boolean b = ordersService.updateById(orders);
        if (b) {
            TotalAmount byId = totalAmountService.getById(orders.getMerchantId());
            if (byId != null) {
                byId.setTotal(byId.getTotal().add(orders.getOrderPrice()));
                boolean b1 = totalAmountService.updateById(byId);
                if (b1) {
                    return 0;
                }
                return -1;
            }
            TotalAmount totalAmount = new TotalAmount();
            totalAmount.setMid(orders.getMerchantId());
            totalAmount.setTotal(orders.getOrderPrice());
            boolean save = totalAmountService.save(totalAmount);
            if (save) {
                return 0;
            }
            return -1;
        }
        return  -1;
    }

    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/getwriteOff/{id}/{code}")
    public AjaxResult getwriteOff(@PathVariable("id") Long id, @PathVariable("code") String code) {

        Orders orders = ordersService.selectOrderverificationRecordByCode(id, code);
        ServiceDetails byId = serviceDetailsService.getById(orders.getServiceId());
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("order", orders);
        ajaxResult.put("service", byId);
        return ajaxResult;

    }
}
